﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

using TomShane.Neoforce.Controls;

using tactics.Abilities;
using tactics.Commands;

namespace tactics.GUI
{
    using FormatMap = Dictionary<string, string>;
    using ScreenCoords = Utility.Pair<int, int>;

    public class AbilityCard : IGUIParsable
    {
        Manager guiManager;

        Panel backPanel;

        TextBox name;
        TextBox type;

        Ability ability;
        FormatMap formats;

        ScreenCoords position;

        #region Properties

        public Ability Ability
        {
            get { return ability; }
        }

        public int Width
        {
            get { return backPanel.Width; }
        }

        public int Height
        {
            get { return backPanel.Height; }
        }

        public bool Visible
        {
            get { return backPanel.Visible; }
            set { backPanel.Visible = value; }
        }

        #endregion

        public AbilityCard(Manager guiManagerIn, string xmlFile, Ability abilityIn)
        {
            InitControls(guiManagerIn, abilityIn);
            GUIBuilder.BuildGUI(this, guiManager, xmlFile, "AbilityCard");
            SetInfoDisplay();
        }

        public void Remove()
        {
            guiManager.Remove(backPanel);
        }

        #region Initialize Controls

        private void InitControls(Manager guiManagerIn, Ability abilityIn)
        {
            guiManager = guiManagerIn;
            ability = abilityIn;

            formats = new FormatMap();

            position = new ScreenCoords();

            backPanel = new Panel(guiManager);
            backPanel.Init();
                
            name = new TextBox(guiManager);
            name.Init();

            type = new TextBox(guiManager);
            type.Init();

            backPanel.Visible = false;            
            name.Passive = true;
            type.Passive = true;

            backPanel.Click += new TomShane.Neoforce.Controls.EventHandler(backPanel_Click);
            backPanel.MouseOver += new MouseEventHandler(backPanel_MouseOver);
            backPanel.MouseOut += new MouseEventHandler(backPanel_MouseOut);
        }

        void backPanel_MouseOut(object sender, MouseEventArgs e)
        {
            CommandQueue.Instance.AddCommand(new EnableDisableMouseCommand(true));
        }

        void backPanel_MouseOver(object sender, MouseEventArgs e)
        {
            CommandQueue.Instance.AddCommand(new EnableDisableMouseCommand(false));
        }

        void backPanel_Click(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            CommandQueue.Instance.AddCommand(new SelectAbilityCommand(ability));
        }

        #endregion

        #region Display
        
        private void SetInfoDisplay()
        {
            name.Text = string.Format(formats["NameText"], ability.Name);
            type.Text = string.Format(formats["TypeText"], ability.Description);
        }

        public void UpdatePosition(int x, int y)
        {
            backPanel.Left = x;
            backPanel.Top = y;

            position.First = x;
            position.Second = y;
        }

        #endregion

        #region Parsing Interface

        public Control Parse(XmlReader r, string controlName)
        {
            Control found = null;

            if (controlName == "BackPanel")
            {
                GUIBuilder.ParseControl<Panel>(r, backPanel);
                found = backPanel;
            }
            else if (controlName == "NameText")
            {
                GUIBuilder.ParseControl<TextBox>(r, name);
                formats[controlName] = name.Text;
                found = name;
            }
            else if (controlName == "TypeText")
            {
                GUIBuilder.ParseControl<TextBox>(r, type);
                formats[controlName] = type.Text;
                found = type;
            }

            return found;
        }        

        #endregion
    }
}
